package com.grq.mis.service;


import cn.hutool.core.lang.Validator;
import com.grq.mis.component.UploadComponent;
import com.grq.mis.dao.CertTypeDao;
import com.grq.mis.dao.ItemDao;
import com.grq.mis.entity.CertType;
import com.grq.mis.entity.Item;
import com.grq.mis.exception.BusinessException;
import com.grq.mis.exception.ResultEnum;
import com.grq.mis.model.sys.Me;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

@Transactional
@Service
public class ItemService extends BaseService<Item>{

    @Autowired
    private ItemDao itemDao;
    @Autowired
    private CertTypeDao certTypeDao;
    @Autowired
    private UploadComponent uploadComponent;

    public void addItem(Item item, Me me, MultipartFile img) {

        CertType certType = certTypeDao.single(item.getCategoryId());
        if (certType == null){
            throw new BusinessException(ResultEnum.ERROR,"商品类型不存在");
        }
        if (certType.getIsDisable()){
            throw new BusinessException(ResultEnum.ERROR,"商品类型不可用");
        }

        item.setCategoryName(certType.getName());

        if (img != null && Validator.isNotEmpty(img.getOriginalFilename())){
            String url = uploadComponent.uploadImg(img);
            item.setMainImg1Url(url);
        }

        item.setCreateTime(new Date());
        item.setCreateUserId(me.getId());
        item.setLeftCount(item.getTotalCount());
        item.setDepartmentId(me.getDepartmentId());
        item.setDepartmentName(me.getDepartmentName());

        itemDao.insert(item);
    }

    public void updateItem(Item model, MultipartFile img,Me me) {

        Item item = itemDao.single(model.getId());
        if (item == null) {
            throw new BusinessException(ResultEnum.ERROR,"商品信息不存在");
        }

        if (!item.getDepartmentId().equals(me.getDepartmentId())){
            if (me.getId() != 1L){
                throw new BusinessException(ResultEnum.ERROR,"无权修改其他商户的商品信息");
            }
        }

        CertType certType = certTypeDao.single(model.getCategoryId());
        if (certType == null){
            throw new BusinessException(ResultEnum.ERROR,"商品类型不存在");
        }
        if (certType.getIsDisable()){
            throw new BusinessException(ResultEnum.ERROR,"商品类型不可用");
        }
        item.setCategoryName(certType.getName());

        if (img != null && Validator.isNotEmpty(img.getOriginalFilename())){
            String url = uploadComponent.uploadImg(img);
            item.setMainImg1Url(url);
        }

        item.setTitle(model.getTitle());
        item.setIntroduction(model.getIntroduction());
        item.setCategoryId(model.getCategoryId());
        item.setPrice(model.getPrice());
        item.setTotalCount(model.getTotalCount());
        item.setLeftCount(model.getLeftCount());

        itemDao.updateById(item);

    }

    public void deleteItem(Long id, Me me) {

        Item item = itemDao.single(id);
        if (item == null) {
            throw new BusinessException(ResultEnum.ERROR,"商品信息不存在");
        }

        if (!item.getDepartmentId().equals(me.getDepartmentId())){
            if (me.getId() != 1L){
                throw new BusinessException(ResultEnum.ERROR,"无权修改其他商户的商品信息");
            }
        }

        itemDao.deleteById(id);

    }
}
